package com.zqweb.gulimall.seckill.controller;

import com.zqweb.common.utils.R;
import com.zqweb.gulimall.seckill.service.SeckillService;
import com.zqweb.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @GetMapping("/getCurrentSeckillSkus")
    @ResponseBody
    // 查询获取当前时间所在的秒杀活动及所关联的所有秒杀商品信息
    public R getCurrentSeckillSkusInfo(){
        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSeckillSkusInfo();
        return R.ok().setData(seckillSkuRedisTos);
    }

    @GetMapping("/getSeckillSkusInfo/{skuId}")
    @ResponseBody
    // 获取指定skuId所关联的秒杀商品（包含优惠信息）
    public R getSeckillSkusInfoBySkuId(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo seckillSkuRedisTo = seckillService.getSeckillSkusInfoBySkuId(skuId);
        return R.ok().setData(seckillSkuRedisTo);
    }

    @GetMapping("/seckillGoods")
    // 秒杀商品（立即抢购）
    public String secKillGoods(@RequestParam("killId") String killId, @RequestParam("killNum") Integer killNum, @RequestParam("killCode") String killCode, Model model){
        String orderSn = seckillService.secKillGoods(killId,killNum,killCode);
        model.addAttribute("orderSn",orderSn);// orderSn可能为空
        return "success";
    }


}
